<?php

include_once 'inc/fire/FirePHP.class.php';

function __autoload($class_name) {
    $fullclasspath = "";
    $suffix = "";
    if (!file_exists('../alania21/inc/' . $fullclasspath . $class_name . ".php")) {
        if (!file_exists('../alania21/inc/' . $fullclasspath . $class_name . ".class.php")) {
            if (file_exists('../alania21/inc/utility/' . $class_name . ".class.php")) {
                $fullclasspath = "utility/";
                $suffix = ".class";
            }
        } else {
            $suffix = ".class";
        }
    }
    //echo '../alania21/inc/' . $fullclasspath . $class_name . $suffix . '.php<br />';
    require_once '../alania21/inc/' . $fullclasspath . $class_name . $suffix . '.php';
}

echo "<pre>";
//$firephp = FirePHP::getInstance(true);
//$firephp->log($_POST['editor1'], 'gettext');
$ht = new XmlDomConstruct();
if ($_POST['preubaselect'] == 1 || $_POST['preubaselect'] == 18) {
    $datos = $ht->html2json($_POST['editor1']);
} else {
    $datos = $ht->body2json($_POST['editor1']);
}
print_r($_POST['preubaselect']);
//echo $datos;

$db = new connection();

$member = AlaniaContenidosModel::findById($db->db, $_POST['preubaselect']);
print_r($member->toArray());
$member->setPlantilla($datos);
$member->updateToDatabase($db->db);
$json = new PRETTY_JSON();
//echo $json->beautify_json($datos);
echo preg_match_all('/(\[\{"#text":\{"attributes":"","hijos":\[\],"0":"\[@)(?<name>(?:[a-z][a-z0-9_]*))(\]"\}\}\])/is', $datos, $matches);
//print_r($matches);
if (count($matches['name'])) {
    print_r(preg_replace('/(\[\{"#text":\{"attributes":"","hijos":\[\],"0":"\[@' . $matches['name'][0] . '\]"\}\}\])/is', '[[@' . $matches['name'][0] . ']]', $datos));
}

class PRETTY_JSON {

    function beautify_json($json) {
        $tab = "  ";
        $new_json = "";
        $indent_level = 0;
        $in_string = false;

        $json_obj = json_decode($json);

        if ($json_obj === false)
            return false;

        $json = json_encode($json_obj);
        $len = strlen($json);

        for ($c = 0; $c < $len; $c++) {
            $char = $json[$c];
            switch ($char) {
                case '{':
                case '[':
                    if (!$in_string) {
                        $new_json .= $char . "\n" . str_repeat($tab, $indent_level + 1);
                        $indent_level++;
                    } else {
                        $new_json .= $char;
                    }
                    break;
                case '}':
                case ']':
                    if (!$in_string) {
                        $indent_level--;
                        $new_json .= "\n" . str_repeat($tab, $indent_level) . $char;
                    } else {
                        $new_json .= $char;
                    }
                    break;
                case ',':
                    if (!$in_string) {
                        $new_json .= ",\n" . str_repeat($tab, $indent_level);
                    } else {
                        $new_json .= $char;
                    }
                    break;
                case ':':
                    if (!$in_string) {
                        $new_json .= ": ";
                    } else {
                        $new_json .= $char;
                    }
                    break;
                case '"':
                    if ($c > 0 && $json[$c - 1] != '\\') {
                        $in_string = !$in_string;
                    }
                default:
                    $new_json .= $char;
                    break;
            }
        }

        return $new_json;
    }

}

?>
